package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 读取文件
 */
public class Demo4 {

	public static void main(String[] args) {
		//copy();
		copyDir();
	}
	
	/*
	 * 拷贝文件夹
	 */
	public static void copyDir(){
		//源目录
		String srcPath = "F:\\11";
		//目标目录
		String destPath = "F:\\pic";
		File srcFile = new File(srcPath);
		
		if(srcFile.isDirectory()){
			File destFile = new File(destPath,srcFile.getName());
			copyDirDetail(srcFile,destFile);
		}
		else{
			copy(srcPath,destPath);
		}
	}
	
	private static void copyDirDetail(File src,File dest){
		if(src.isFile()){
			copy(src,dest);
		}
		else{
			dest.mkdirs();
		    for(File temp:src.listFiles()){
		    	copyDirDetail(temp,new File(dest,temp.getName()));
		    }
		}
	}
	
	public static void copy(File src,File dest){
		copy(src.getAbsolutePath(),dest.getAbsolutePath());
	}
	
	/*
	 * 拷贝文件
	 */
	public static void copy(String srcPath,String destPath){
		//建立文件联系
		//String path = "F:\\pic\\3.jpg";
		File file = new File(srcPath);
		File dest = new File(destPath);
		if(file.isDirectory()){
			return;
		}
		//选择流
		FileOutputStream os = null;
		FileInputStream is = null;
		try {
			os = new FileOutputStream(dest,false);
			is = new FileInputStream(file);
			//文件拷贝
			byte[] buffer = new byte[1024];
			int len = 0;
			while(-1 != (len = is.read(buffer))){
				//写出
				os.write(buffer, 0, len);
			}
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != os){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != is){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	public static void write(String str){
		//建立文件联系
		String path = "F:\\pic\\5.txt";
		File file = new File(path);
		//选择流
		FileOutputStream is = null;
		try {
			is = new FileOutputStream(file,false);
		    byte[] data = str.getBytes();
		    is.write(data);
		    //如果管道未满，不会自动提交
		    is.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != is){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void read(){
		//建立文件联系
		String path = "F:\\pic\\3.jpg";
		File file = new File(path);
		//选择流
		FileInputStream is = null;
		try {
			is = new FileInputStream(file);
			//操作不断读取，缓冲数组
		    byte[] buffer = new byte[10];
		    //实际读取大小
		    int len = 0;
		    
		    while(-1 != (len = is.read(buffer))){
		    	//输出，字节数组转字符串
		    	String info = new String(buffer,0,len);
		    	System.out.println(info);
		    }
		    
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != is){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
